package net.toocruel.yarn.mapreduce.wordcount;

import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.FileSystem;
import org.apache.hadoop.fs.Path;
import org.apache.hadoop.io.IntWritable;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.Job;
import org.apache.hadoop.mapreduce.lib.input.FileInputFormat;
import org.apache.hadoop.mapreduce.lib.output.FileOutputFormat;

/**
 * wordcount Standalone 模式 运行
 * @author : 宋同煜
 * @version : 1.0
 * @createTime : 2017/4/12 14:15
 * @description :
 */
public class WordCountStandalone {
    public static void main(String[] args)throws Exception {
        //初始化配置
        Configuration conf = new Configuration();
        //创建一个job提交器对象
        Job job = Job.getInstance(conf);
        job.setJobName("WordCount");

        //设置map，combine，reduce处理
        job.setMapperClass(WordCountMapper.class);
        job.setReducerClass(WordCountReducer.class);

        //设置输出格式处理类
        job.setOutputKeyClass(Text.class);
        job.setOutputValueClass(IntWritable.class);

        //设置输入输出路径
        FileSystem.get(new Configuration()).delete(new Path("temp/output")); //先清空输出目录
        FileInputFormat.addInputPath(job, new Path("temp/a.txt"));
        FileOutputFormat.setOutputPath(job, new Path("temp/output"));

        boolean res =  job.waitForCompletion(true);
        System.out.println("任务名称: "+job.getJobName());
        System.out.println("任务成功: "+(res?"Yes":"No"));
        System.exit(res?0:1);
    }
}
